package com.yjhealth.libs.core.databinding;

import android.databinding.BindingAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.yjhealth.libs.core.utils.DensityUtil;
import com.yjhealth.libs.core.utils.EffectUtil;


/**
 * Created on 2019/11/20 10:50
 *
 * @author cyf
 * @des :
 */

public class ViewBindingAdapter {
    public static boolean NO_EFFECT = false;
    public static boolean EFFECT = true;

    @BindingAdapter(value = {"android:onClickListener", "android:clickable", "effect"}, requireAll = false)
    public static void setClickListener(View view, View.OnClickListener clickListener,
                                        Boolean clickable, Boolean effect) {
        clickListener(view, clickListener, clickable, effect);
    }

    @BindingAdapter(value = {"android:onClick", "android:clickable", "effect"}, requireAll = false)
    public static void setOnClick(View view, View.OnClickListener clickListener,
                                  Boolean clickable, Boolean effect) {
        clickListener(view, clickListener, clickable, effect);
    }

    private static void clickListener(View view, View.OnClickListener clickListener, Boolean clickable, Boolean effect) {
        if (effect == null || effect) {
            EffectUtil.addClickEffect(view);
        }
        view.setOnClickListener(clickListener);
        if (clickable != null) {
            view.setClickable(clickable);
        }
    }

    /**
     * unit dip
     * @param view
     * @param marginLeft
     * @param marginStart
     * @param marginRight
     * @param marginEnd
     * @param marginTop
     * @param marginBottom
     */
    @BindingAdapter(value = {"android:layout_marginLeft", "android:layout_marginStart",
            "android:layout_marginRight", "android:layout_marginEnd",
            "android:layout_marginTop", "android:layout_marginBottom"}, requireAll = false)
    public static void setLayoutMargin(View view, Integer marginLeft, Integer marginStart,
                                       Integer marginRight, Integer marginEnd, Integer marginTop, Integer marginBottom) {
        if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
            int left = p.leftMargin;
            if(marginLeft != null || marginStart != null) {
                left = marginLeft != null ? DensityUtil.dip2px(marginLeft) : DensityUtil.dip2px(marginStart);
            }
            int right = p.rightMargin;
            if(marginRight != null || marginEnd != null) {
                right = marginRight != null ? DensityUtil.dip2px(marginRight) : DensityUtil.dip2px(marginEnd);
            }
            int top = marginTop != null ? DensityUtil.dip2px(marginTop) : p.topMargin;
            int bottom = marginBottom != null ? DensityUtil.dip2px(marginBottom) : p.bottomMargin;
            
            p.setMargins(left, top, right, bottom);
            view.requestLayout();
        }
    }

}
